#!/bin/bash

# 零售平台Docker Compose启动脚本
# 作者: retail
# 日期: 2024-01-01

set -e

echo "🚀 启动零售平台Docker Compose环境..."


# 检查Docker是否运行
if ! docker info > /dev/null 2>&1; then
    echo "❌ Docker未运行，请先启动Docker"
    exit 1
fi

# 检查docker compose是否可用
if ! docker compose version > /dev/null 2>&1; then
    echo "❌ docker compose未可用，请确保Docker版本支持compose"
    exit 1
fi

# 进入项目根目录
cd "$(dirname "$0")/.."

echo "📦 构建并启动所有服务..."

# 停止并删除现有容器
echo "🛑 停止现有容器..."
docker compose -f docker/docker-compose.yml -p retail-platform down

# 构建并启动服务
echo "🔨 构建并启动服务..."
docker compose -f docker/docker-compose.yml -p retail-platform up --build -d

# 等待服务启动
echo "⏳ 等待服务启动..."
sleep 30

# 检查服务状态
echo "🔍 检查服务状态..."
docker compose -f docker/docker-compose.yml -p retail-platform ps

# 显示访问信息
echo ""
echo "✅ 零售平台启动完成！"
echo ""
echo "📋 服务访问地址："
echo "  🌐 前端应用: http://localhost:3000"
echo "  🔧 后端API: http://localhost:8080"
echo "  🌍 Nginx代理: http://localhost:80"
echo "  🗄️  MySQL: localhost:3306"
echo "  📦 Redis: localhost:6379"
echo ""
echo "📊 健康检查："
echo "  🔍 后端健康: http://localhost:8080/retail-admin/api/auth/health"
echo "  🔍 前端健康: http://localhost:3000"
echo ""
echo "📝 查看日志："
echo "  docker compose -f docker/docker-compose.yml -p retail-platform logs -f [service_name]"
echo ""
echo "🛑 停止服务："
echo "  docker compose -f docker/docker-compose.yml -p retail-platform down"
echo ""
